//
//  TittleDetailViewController.m
//  半糖
//
//  Created by RenShuang on 15-9-3.
//  Copyright (c) 2015年 shuang. All rights reserved.
//

#import "TittleDetailViewController.h"
#import "AFNBlock.h"
#import "firstTableModel.h"
#import "firstTableViewCell.h"
#import "BaseHeader.h"
#import "firstTitleDetailCollectionModel.h"
#import "FirstCollectionViewCell.h"

typedef void (^myBlock)(id block);

@interface TittleDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *collectionArr;
@property(nonatomic,retain)NSMutableArray *tableArr;
@property(nonatomic,retain)UICollectionView *myCollectionView;
@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,copy)NSString *webStr;

@end

@implementation TittleDetailViewController

- (void)dealloc
{
    [_myTableView release];
    [_myCollectionView release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.myTableView reloadData];
    [self.myCollectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSString *str = [self.model.extend stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
    NSString *urlStr = [NSString stringWithFormat:@"http://open3.bantangapp.com/topic/list?app_installtime=1440915950.090587&app_versions=4.1&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&ids=%@&oauth_token=6de3bace25123737e17db3e839477eb1&os_versions=8.2&page=0&pagesize=20&screensize=640&track_device_info=iPhone&track_deviceid=B4391E1B-981F-4473-A960-431F69E76E8C&track_user_id=1260331&v=6",str];
    
    
    _collectionArr = [[NSMutableArray alloc] init];
    _tableArr = [[NSMutableArray alloc] init];
    
    
    
    NSString *typeStr = self.model.type;
    
    if ([typeStr isEqualToString:@"product_list"]) {
        
        NSString *collectionUrl = [NSString stringWithFormat:@"http://open3.bantangapp.com/product/list?app_installtime=1440915950.090587&app_versions=4.1.1&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&ids=%@&oauth_token=6de3bace25123737e17db3e839477eb1&os_versions=8.2&page=0&pagesize=20&screensize=640&track_device_info=iPhone&track_deviceid=B4391E1B-981F-4473-A960-431F69E76E8C&track_user_id=1260331&v=6",str];
        NSLog(@"%@",collectionUrl);
        [self collectionViewCreateWithStr:collectionUrl];
        
    }else if ([typeStr isEqualToString:@"webview"]){
        self.webStr = self.model.extend;
        [self webViewCreate];
        
    }else if ([typeStr isEqualToString:@"activity"]){
        self.webStr = [NSString stringWithFormat:@"%@?random=6de3bace25123737e17db3e839477eb1&user_id=1260331&type=1",self.model.extend];
        [self webViewCreate];
    }else if ([typeStr isEqualToString:@"topic_detail"]){
        self.webStr = [NSString stringWithFormat:@"http://m.ibantang.com/topic/detail/%@/",self.model.extend];
        [self webViewCreate];
    }
    else{
        
        [self tableViewCreateWithStr:urlStr];
    }
    
    [self barCreate];

}


- (void)barCreate
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    titleView.backgroundColor = [UIColor redColor];
    [self.view addSubview:titleView];
    [titleView release];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4, 20, SCREEN_WIDTH/2, 44)];
    //titleLabel.backgroundColor = [UIColor greenColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = self.model.title;
    //label加粗并放大
    titleLabel.font = [UIFont boldSystemFontOfSize:20.];
    [titleView addSubview:titleLabel];
    [titleLabel release];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(20, 30, 30, 25);
    [backButton setBackgroundImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:backButton];
}

- (void)collectionViewCreateWithStr:(NSString *)str
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH-30)/2, (SCREEN_WIDTH-30)*3/4);
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);

    
    _myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    _myCollectionView.backgroundColor = [UIColor colorWithRed:250/255. green:250/255. blue:250/255. alpha:1];
    [self.view addSubview:_myCollectionView];
    
    [AFNBlock getDataWithString:str block:^(id block) {
        for (NSDictionary *dic in [block objectForKey:@"data"]) {
            firstTitleDetailCollectionModel *model = [[firstTitleDetailCollectionModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            model.cid = [dic objectForKey:@"id"];
            [_collectionArr addObject:model];
        }
        [_myCollectionView reloadData];
    }];
    
    [_myCollectionView registerClass:[FirstCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}

- (void)webViewCreate
{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT-40)];
    [webView setScalesPageToFit:YES];
    [self.view addSubview:webView];
    
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webStr]]];
    [webView release];
}

- (void)tableViewCreateWithStr:(NSString *)str
{
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    _myTableView.delegate = self;
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
    
    [AFNBlock getDataWithString:str block:^(id block) {
        for (NSDictionary *dic in [[block objectForKey:@"data"] objectForKey:@"topic"]) {
            firstTableModel *model = [[firstTableModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            model.cid = [dic objectForKey:@"id"];
            [_tableArr addObject:model];
        }
        [_myTableView reloadData];
    }];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _collectionArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [NightHandle shareHandle].viewColor;
    cell.titleLabel.textColor = [NightHandle shareHandle].wordColor;
    firstTitleDetailCollectionModel *model = [_collectionArr objectAtIndex:indexPath.row];
    [cell getCell:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WebViewController *web = [[WebViewController alloc] init];
    firstTitleDetailCollectionModel *model = [_collectionArr objectAtIndex:indexPath.row];
    web.urlStr = model.cid;
    [self.navigationController pushViewController:web animated:YES];
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT*7/24;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid = @"cell";
    firstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[firstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.contentView.backgroundColor = [NightHandle shareHandle].viewColor;
    firstTableModel *model = [_tableArr objectAtIndex:indexPath.row];
    [cell getcell:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableDetailViewController *t = [[TableDetailViewController alloc] init];
    firstTableModel *model = [_tableArr objectAtIndex:indexPath.row];
    t.str = model.cid;
    t.picStr = model.pic;
    t.titleStr = model.title;
    NSLog(@"%@",model.cid);
    [self.navigationController pushViewController:t animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
